package cvi.com.reader.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orhanobut.hawk.Hawk;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.bean.PictureBookBean;
import cvi.com.reader.bean.constant;

/**
 * Created by 梅涛 on 2020/7/23.
 */

public class PictureBookAdapter extends BaseAdapter {
    private Context context;
    private List<HotBean.RowsBean> gridViewBeanList;
    private CollectListener collectListener;

    public CollectListener getCollectListener() {
        return collectListener;
    }

    public void setCollectListener(CollectListener collectListener) {
        this.collectListener = collectListener;
    }

    public PictureBookAdapter(Context context, List<HotBean.RowsBean> gridViewBeanList) {
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public int getCount() {
        return gridViewBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return gridViewBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viHolder = null;
        final HotBean.RowsBean gridViewBean = gridViewBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_grid_picture_book, null);
            viHolder = new ViewHolder(view);
            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }
        if (0 == gridViewBean.getIs_collected()) {
            viHolder.collectBtn.setBackgroundResource(R.drawable.ic_collect_un);
            viHolder.collectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    collectListener.onCollectClick("1", gridViewBean.getId());//调用收藏接口

                }
            });
        } else {
            viHolder.collectBtn.setBackgroundResource(R.drawable.ic_collect);
            viHolder.collectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    collectListener.onCollectClick("0", gridViewBean.getId());//调用取消收藏接口
                }
            });
        }
        viHolder.workName.setText(gridViewBean.getMaterial_name());
        viHolder.secondFloor.getBackground().setAlpha(178);
      /*  String strUrl = null;
        if (Hawk.contains("ip")) {
            strUrl = Hawk.get("ip");
        }*/
        String[] str = null;
        if (gridViewBean.getMedia_path().contains(",")) {
            str = gridViewBean.getMedia_path().split(",");
        } else if (gridViewBean.getMedia_path().contains("，")) {
            str = gridViewBean.getMedia_path().split("，");
        }
        ImageLoader.getInstance().displayImage("http://" +  constant.URL + str[0], viHolder.image);
       /* if (strUrl != null) {
            ImageLoader.getInstance().displayImage("http://" + strUrl + str[0], viHolder.image);
        }*/
        return view;
    }

    public interface CollectListener {
        public void onCollectClick(String flag, int id);
    }

    static class ViewHolder {
        @BindView(R.id.collect_btn)
        Button collectBtn;
        @BindView(R.id.work_name)
        TextView workName;
        @BindView(R.id.second_floor)
        RelativeLayout secondFloor;
        @BindView(R.id.rl_all)
        RelativeLayout rlAll;
        @BindView(R.id.image)
        ImageView image;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
